package cm.mefobe.android.telecomcost.view;

import cm.mefobe.android.telecomcost.entities.Time;
import android.app.TimePickerDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimePickerView extends TableLayout {

	private Context _context;
	private TextView _label;
	private EditText _txtTime;
	private Button _btnSetTime;
	private TimePickerDialog dialog;
	private Time _time;
	private TableRow row;

	public Time getTime() {
		return _time;
	}
	
	public boolean isTimeSet()
	{
		return _time != null;
	}

	public void setTime(Time _time) {
		this._time = _time;
		_txtTime.setText(_time.toString());
	}

	public TimePickerView(Context context, String label) {
		super(context);
		Log.i("TimePickerView", "creating the time picker view for " + label);
		_context = context;
		_label.setText(label);
	}

	private void setTimePickerView() {
		if (isInEditMode()) {
			Log.i("TimePickerView", "isInEditMode");
		} else {
			if (row == null) {
				Log.i("TimePickerView", "adding views...");
				row = new TableRow(_context);
				_label = new TextView(_context);
				_txtTime = new EditText(_context);
				_btnSetTime = new Button(_context);

				_label.setText("Start Time : ");				
				_txtTime.setEnabled(false);

				_btnSetTime.setText("Pick");
				_btnSetTime.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showTimePickerDialog();
					}
				});
				
				_txtTime.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showTimePickerDialog();
					}
				});

				row.addView(_label);
				row.addView(_txtTime);
				row.addView(_btnSetTime);

				addView(row);	
				setColumnStretchable(0,true);
				setTime(new Time(0, 0));
			} else {
				Log.i("TimePickerView", "TimePickerView already set");
			}
		}
	}

	public TimePickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("TimePickerView",
				"creating the time picker view with AttributeSet");

		_context = context;		
	}

	public TimePickerView(Context context) {
		super(context);
		Log.i("TimePickerView", "creating the time picker view");
		_context = context;
	}

	protected void showTimePickerDialog() {
		dialog = new TimePickerDialog(_context, mTimeSetListener, 0, 0, true);
		dialog.show();
	}

	// the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			_time = new Time(hourOfDay, minute);
			_txtTime.setText(_time.toString());
		}
	};

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("TimePickerView", "onLayout");
		super.onLayout(changed, l, t, r, b);
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("TimePickerView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("TimePickerView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("TimePickerView", "onFinishInflate");
		super.onFinishInflate();
		setTimePickerView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("TimePickerView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public void setLabel(String label) {
		if (_label != null) {
			_label.setText(label);
		} else {
			Log.i("TimePickerView", "cannot set the label...textView is null");
		}
	}
}
